package com.tanhua.manager.listener;

import com.alibaba.fastjson.JSON;
;
import com.tanhua.manager.domain.Log;
import com.tanhua.manager.service.LogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

@Slf4j
@Component
@RocketMQMessageListener(topic = "logTopic",consumerGroup = "logGroup")
public  class LogListener implements RocketMQListener<String> {
    @Autowired
    private LogService logService;

    @Override
    public void onMessage(String message) {
//        log.info(message);
        try {
            Map map = JSON.parseObject(message, Map.class);
            Long userId = Long.valueOf(map.get("userId").toString());
            String type = (String) map.get("type");
            String date = (String) map.get("date");
            System.out.println("接受的消息:   "+new Date()+message);

            Log logObj = new Log();
            logObj.setUserId(userId);
            logObj.setLogTime(date);
            logObj.setType(type);
            logObj.setCreated(new Date());
            logObj.setUpdated(new Date());
            logService.save(logObj);
        } catch (Exception e) {
            log.error("消费消息", e);
        }
    }
}
